use strict;

my $dir = $ARGV[0];
my $sample = $ARGV[1];

opendir(DIR, $dir);
my @users = grep !/^\./, readdir(DIR);
closedir(DIR);

my $cnt = 0;

# not memory safe
foreach (@users) {
    # recursive 
    #print STDERR "cnt is ".read_mail_dir("$dir/$_")."\n";
    $cnt += read_mail_dir("$dir/$_");
}

print STDERR "cnt is $cnt\n";

sub read_mail_dir {
    my ($dir) = @_;
    my $cnt = 0;
    opendir(DIR, $dir);
    my @subs = grep !/^\./, readdir(DIR);
    closedir(DIR);
    foreach my $file (@subs) {
        if (-d "$dir/$file") {
            $cnt += read_mail_dir("$dir/$file");
        } else {
            # flip a coin
            if (rand() <= $sample) {
              parse("$dir/$file");
              $cnt++;
            }
        }
    }
    return $cnt;
    
}

sub parse {
    my ($path) = @_;

    open(FILE, $path);

    my ($from, $to, $body, $done);
    $done = 0;
    while (<FILE>) {    
        #print;
        if ($done) {
            $body .= $_;
        } else {
          if (/^\s*From:\s*(\S+)/) {
            $from = test_email($1);
          } elsif (/^\s*To:\s*(\S+)/) {
            $to = test_email($1);
          } elsif (/^X-FileName:/) {
            $done = 1;
            next;
          }
        }
    }
    close(FILE);

    $body =~ s/'/''/g;
    # not ideal
    $body =~ s/"//g;
    $body =~ s/\s+/ /g;
    $body =~ s/\s+/ /g;
    print "$from,$to,\"$body\"\n";
    #print "*** $from,$to,****\n";
}

sub test_email {
    my ($str) = @_;
    
    my $email_pat = /[^@]+@\S+/;
    if ($str =~ /[^@]+@\S+/) {
    } elsif ($str =~ /<\s*([^@]+@\S+)\s*>/) {
        $str = $1;
    }

    $str =~ s/,//g;
    $str =~ s/"//g;
    $str =~ s/'//g;
    #die "no good: $str\n";
    # doesn't seem like a valid email address.  oh well

    print STDERR "$str\n";
    return $str;

}
